package com.diary.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diary.common.constant.JwtClaimsConstant;
import com.diary.common.properties.JwtProperties;
import com.diary.common.result.Result;
import com.diary.common.utils.JwtUtil;
import com.diary.server.dto.LoginDTO;
import com.diary.server.entity.DiaryList;
import com.diary.server.entity.User;
import com.diary.server.service.impl.DiaryServiceImpl;
import com.diary.server.service.impl.UserServiceImpl;
import com.diary.server.vo.LoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
@Tag(name = "用户接口")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private DiaryServiceImpl diaryService;

    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("/login")
    @Operation(summary = "用户登录")
    public Result<LoginVO> login(String code) {
        LoginDTO userLoginDTO = new LoginDTO();
        userLoginDTO.setCode(code);
        User user = userService.wxLogin(userLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());


        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        LoginVO userLoginVO = LoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenId())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }
}
